//
//  YLARewardedView.swift
//  Your Lover AI
//
//  Created by 阿忠 on 2024/7/28.
//

import Foundation
import UIKit

/**
 修改名字的弹窗
 */
class YLARewardedView: UIView {
    private var titleLabel = UILabel()
    private let contentLabel = UILabel()
    private let tistLabel = UILabel()
    
    private let cancelButton = UIButton()
    private let confirmButton = UIButton()
    
    var onCancel: (() -> Void)?
    var onConfirm: ((String?) -> Void)?
    
    init(frame: CGRect, title: String, paymentMethod: String, paymentDescription: String) {
        super.init(frame: frame)
        
        setupUI(title: title, paymentMethod: paymentMethod, paymentDescription: paymentDescription)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(title: String, paymentMethod: String, paymentDescription: String) {
        // 设置自定义弹窗的样式和布局
        self.backgroundColor = .white
        self.layer.cornerRadius = 20
     
        titleLabel.text = "Get conversation count"
        titleLabel.textColor = .black
        titleLabel.font = UIFont.systemFont(ofSize: 18)
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 0
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(29)
            make.height.equalTo(18)
            make.centerX.equalToSuperview()
        }
        
        contentLabel.text = "Get the number of conversations by watching the rewarded video, and add 3 new conversations each time you watch it."
        contentLabel.textColor = UIColor(hex: "303030")
        contentLabel.font = UIFont.systemFont(ofSize: 15)
        contentLabel.textAlignment = .left
        contentLabel.numberOfLines = 0
        self.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(16)
            make.left.equalTo(20)
            make.right.equalTo(-20)
        }
        
        let userDefaults = UserDefaults.standard
        let todaStr = "Today's times："
        var isUserFreeCount = userDefaults.string(forKey: "isWatchCount")
        let numberStr = isUserFreeCount
        
        tistLabel.text = todaStr + (numberStr ?? "10")
        tistLabel.textColor = UIColor(hex: "303030")
        tistLabel.font = UIFont.systemFont(ofSize: 15)
        tistLabel.textAlignment = .center
        tistLabel.numberOfLines = 0
        self.addSubview(tistLabel)
        tistLabel.snp.makeConstraints { make in
            make.top.equalTo(contentLabel.snp.bottom).offset(16)
            make.left.equalTo(20)
            make.right.equalTo(-20)
        }
        
       
        
        
        let screenWidth = UIScreen.main.bounds.width
        cancelButton.setTitle("No", for: .normal)
        cancelButton.setTitleColor(.black, for: .normal)
        cancelButton.layer.borderWidth = 0.5
        cancelButton.layer.borderColor = UIColor.black.cgColor
        cancelButton.backgroundColor = .clear
        cancelButton.layer.cornerRadius = 20
        self.addSubview(cancelButton)
        cancelButton.snp.makeConstraints { make in
            make.bottom.equalTo(-20)
            make.left.equalTo(20)
            make.width.equalTo(88)
            make.height.equalTo(46)
        }
        
        confirmButton.setTitle("Watch", for: .normal)
        confirmButton.setTitleColor(.white, for: .normal)
        confirmButton.backgroundColor = UIColor(hex: "AB00E4")
        confirmButton.layer.cornerRadius = 20
        self.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.bottom.equalTo(-20)
            make.right.equalTo(-20)
            make.left.equalTo(cancelButton.snp.right).offset(15)
            make.height.equalTo(46)
        }
        
        // 取消按钮点击事件
        cancelButton.addTarget(self, action: #selector(cancelButtonTapped), for: .touchUpInside)
        
        // 确定按钮点击事件
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
        
        let number = Int(isUserFreeCount ?? "10")
        if number! <= 0 {
            confirmButton.backgroundColor = .gray
            confirmButton.isEnabled = false
        }else{
            confirmButton.backgroundColor = UIColor(hex: "AB00E4")
            confirmButton.isEnabled = true
        }
        
    }
        
    @objc private func cancelButtonTapped() {
        onCancel?()
    }
    
    @objc private func confirmButtonTapped() {
        onConfirm?("1")
    }
}

